<?php

namespace App\Http\Controllers\Game\Setting;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Game\Setting\Game\ItemRequest;
use App\Http\Resources\Common\DatasResource;
use App\Http\Resources\Common\DataResource;
use App\Models\Game\Config\Game;
use App\Services\Game\Setting\GameService;

class GameController extends Controller
{

    /**
     * 获取数据列表
     */
    public function getList(Request $request)
    {
        list ($collection, $total) = GameService::make()->getList($request);

        return DatasResource::make($collection)->setPaginateTotal($total);
    }

    /**
     * 保存数据
     */
    public function saveItem(ItemRequest $request)
    {
        $game = Game::withTrashed()->updateOrCreate(
            $request->only(['id']),
            $request->only([
                'index', 'name', 'type', 'code',
                'category', 'grade', 'base_user_num', 'private',
            ])
        );
        // 通知游戏端配置修改
        $game->syncGameService();

        return new DataResource($game);
    }

    /**
     * 删除数据
     */
    public function destroy(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer|min:1'
        ]);
        $id = $request->input('id');

        $game = Game::findOrFail($id);
        $game->delete();
        // 通知游戏端配置修改
        $game->syncGameService();

        return new DataResource($game);
    }

    /**
     * 恢复删除
     */
    public function restore(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer|min:1'
        ]);
        $id = $request->input('id');

        $game = Game::withTrashed()->findOrFail($id);
        $game->restore();
        // 通知游戏端配置修改
        $game->syncGameService();

        return new DataResource($game);
    }

    /**
     * 彻底删除
     */
    public function forceDestroy(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer|min:1'
        ]);
        $id = $request->input('id');

        $game = Game::withTrashed()->findOrFail($id);
        $game->forceDelete();
        // 通知游戏端配置修改
        $game->syncGameService();

        return new DataResource($game);
    }
}
